package com.test.ml.yqs.service;


import com.test.ml.yqs.entity.BetCode;
import com.test.ml.yqs.entity.SystemVariable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * 重庆时时彩计划
 * 
 * @author pcy
 *
 */
@Service
public class CqsscdsPlanService {
	@Autowired
	private CqsscBetService betService;
	@Autowired
	private RestTemplate restTemplate;

	@Value("${plan.lcyx.url}")
	private  String lcyxPlanUrl;
	@Value("${plan.lcyxds.url}")
	private  String lcyxdanshuangPlanUrl;
	private  int lcyxPetMoney=3;
	private static String updx;//上期投的大小
	private static String betpid;//下注的期数
	private static int lxnum=0;//连续输的次数
	private int DStotalwinMoney=0;
	/**
	 * 流城 一星 单双 3期计划
	 */
	public boolean lcyxdanshuangPlan(int beginMoney,int cycleNum) {
		//如果本期已下注
		if(betpid!=null && betpid.equals(SystemVariable.pid)){
			return false;
		}
		String resultStr = restTemplate.getForObject(lcyxdanshuangPlanUrl, String.class, "");
		if(resultStr==null || resultStr.equals("")){
			return false;
		}
		int index = resultStr.indexOf("id=\"tzh\">");
		// 计划号码大小
		String plancodedx = resultStr.substring(index + 9, index + 14);

		String tempstr=null;
		try {
			tempstr = resultStr.substring(index - 60, index);
		} catch (Exception e) {
			System.out.println(resultStr.length()+"xxxxxx"+resultStr);
			e.printStackTrace();
		}
		
		int pidindex = tempstr.lastIndexOf("┊万┊");
		// 计划期数
		String planpid = tempstr.substring(pidindex - 7, pidindex);
		System.out.println("+++++++"+plancodedx+":"+planpid);
		
		if(SystemVariable.pid!=null && planpid!=null){
			int minPlanPid=Integer.parseInt(planpid.substring(0,3));
			int maxPlanPid=Integer.parseInt(planpid.substring(4,7));
			int pid=Integer.parseInt(SystemVariable.pid.substring(SystemVariable.pid.length()-3));
			//在计划期内
			if(pid>=minPlanPid && pid<=maxPlanPid){
				//本期投第一球的大小
				String dx=plancodedx.equals("02468")?"双":"单";
				String betcode1=null;
				if(plancodedx==null || plancodedx.equals("")){
					return false;
				}else if(plancodedx.equals("02468")){
					betcode1= BetCode.Q1_SHUANG;
				}else if(plancodedx.equals("13579")){
					betcode1=BetCode.Q1_DAN;
				}else{
					return false;
				}
				
				int code_1=Integer.parseInt(SystemVariable.upopcode.substring(0, 1));
				String opencode1_ds="02468".contains(code_1+"")?"双":"单";
				if(updx==null){
					System.out.println("last time dont bet，begin bet");//上期未投，开始投注
					lcyxPetMoney=beginMoney;
				}else if(updx.equals(opencode1_ds)){
//					System.out.println("上期中了！上期中了:"+lcyxPetMoney);
					System.out.println("last time win ! win money :"+lcyxPetMoney);
					DStotalwinMoney+=lcyxPetMoney;
					lcyxPetMoney=beginMoney;
					lxnum=0;
				}else{
//					System.out.println("上期输了！输了"+lcyxPetMoney);
					System.out.println("last time lose !! lose money : "+lcyxPetMoney);
					DStotalwinMoney-=lcyxPetMoney;
					//如果连续失败次数超过3次，则开始新的一轮
					if(++lxnum >= cycleNum){
						lcyxPetMoney=beginMoney;
						lxnum=0;
						
					}else{
						lcyxPetMoney=lcyxPetMoney * 3;
					}
				}
//				System.out.println("本次投"+lcyxPetMoney);
//				System.out.println("第"+pid+"期，第一球买"+dx+":"+lcyxPetMoney);
				
				System.out.println("this time bet money :"+lcyxPetMoney);
				
				System.out.println("total danshang win moeny :"+DStotalwinMoney +" RMB");
				System.out.println("the "+pid+"period，the frist ball bei"+(dx.equals("双")?"shuang":"dan") +"for money:"+lcyxPetMoney);
				boolean result = betService.postbet(betcode1, lcyxPetMoney+"");
				if(result){
					
					updx=dx;
					betpid=SystemVariable.pid;
				}else{
					lcyxPetMoney=beginMoney;
					lxnum=0;
				}
//				lcyxPlanSave(dx, lcyxPetMoney+"");
			}
		}
		return true;
	}

}
